/*
 * Copyright 2012 Seitenbau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.seitenbau.jenkins.plugins.dynamicparameter.scriptler;

import hudson.Extension;
import hudson.model.ParameterValue;
import hudson.model.StringParameterValue;

import java.util.Map;
import java.util.Set;

import org.jenkinsci.plugins.scriptler.config.Script;
import org.jvnet.localizer.ResourceBundleHolder;
import org.kohsuke.stapler.DataBoundConstructor;

import com.seitenbau.jenkins.plugins.dynamicparameter.util.JenkinsUtils;

/** Text parameter, with dynamically generated default value. */
public class ScriptlerStringParameterDefinition extends ScriptlerParameterDefinition
{
  /** Serial version UID. */
  private static final long serialVersionUID = 3473431531782581400L;

  private final Boolean readonlyInputField;

  /**
   * Constructor.
   * @param name parameter name
   * @param description parameter description
   * @param uuid identifier (optional)
   * @param scriptlerScriptId Scriptler script id
   * @param parameters script parameters
   * @param remote execute the script on a remote node
   */
  @DataBoundConstructor
  public ScriptlerStringParameterDefinition(String name, String description, String uuid,
      String scriptlerScriptId, ScriptParameter[] parameters, Boolean remote, Boolean readonlyInputField)
  {
    super(name, description, uuid, scriptlerScriptId, parameters, remote);
    this.readonlyInputField = readonlyInputField;
  }

  /**
   * Return default parameter value - used by trigger mechanism.
   */
  @Override
  public ParameterValue getDefaultParameterValue() {
    StringParameterValue stringParameterValue = new StringParameterValue(getName(), getDefaultValue());
    return stringParameterValue;
  }

  /**
   * Execute the script and return the default value for this parameter.
   * @return the default value generated by the script or {@code null}
   */
  public final String getDefaultValue()
  {
	Map<String, String> parameters = getParametersAsMap();
    return getScriptResultAsString(parameters);
  }

  public boolean isReadonlyInputField()
  {
    if(readonlyInputField == null)
    {
      return false;
    }
    return readonlyInputField;
  }

  /** Parameter descriptor. */
  @Extension
  public static final class DescriptorImpl extends ParameterDescriptor
  {
    private static final String DISPLAY_NAME = "DisplayName";

    @Override
    public final String getDisplayName()
    {
      return ResourceBundleHolder.get(ScriptlerStringParameterDefinition.class)
          .format(DISPLAY_NAME);
    }

    public Set<Script> getScripts()
    {
      return JenkinsUtils.getAllScriptlerScripts();
    }
  }

}
